package com.cqcet.wenchuang.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqcet.wenchuang.common.R;
import com.cqcet.wenchuang.dto.CollectionDto;
import com.cqcet.wenchuang.entity.Collection;
import com.cqcet.wenchuang.entity.Goods;
import com.cqcet.wenchuang.service.CollectionService;
import com.cqcet.wenchuang.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 收藏
 */
@Slf4j
@Api(tags = "收藏管理接口")
@RequestMapping("/Collections")
@RestController
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private GoodsService goodsService;

    /**
     * 新增收藏
     *
     * @param collection
     * @return
     */
    @ApiOperation(value = "新增收藏", notes = "必要:userId,goodsId,adminId")
    @PostMapping
    public R<String> save(@RequestBody Collection collection) {
        collectionService.save(collection);
        return R.success("收藏成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除收藏", notes = "必要:id")
    @DeleteMapping("/{id}")
    public R<String> remove(@PathVariable Long id) {
        collectionService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 用户收藏
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "全部收藏", notes = "必选:userId")
    @GetMapping("/{userId}")
    public R<List<CollectionDto>> list(@PathVariable Long userId) {
        LambdaQueryWrapper<Collection> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户id查询收藏
        queryWrapper.eq(!ObjectUtils.isNotEmpty(userId), Collection::getUserId, userId);
        //根据更细时间，创建时间降序排序
        queryWrapper.orderByDesc(Collection::getUpdateTime).orderByDesc(Collection::getCreateTime);

        List<Collection> collectionList = collectionService.list(queryWrapper);

        List<CollectionDto> collectionDtos = collectionList.stream().map(collection -> {
            CollectionDto collectionDto = new CollectionDto();
            Goods goods = goodsService.getById(collection.getGoodsId());
            collectionDto.setGoods(goods);
            BeanUtils.copyProperties(collection, collectionDto);
            return collectionDto;
        }).collect(Collectors.toList());

        return R.success(collectionDtos);
    }

    @ApiOperation(value = "查看收藏数量", notes = "管理员或者商家查看收藏商品数量(无需关心)")
    @PostMapping("/count")
    public R<String> count(Collection collection) {
        LambdaQueryWrapper<Collection> queryWrapper = new LambdaQueryWrapper<>();
        //根据商品id查看该商品的收藏数量
        queryWrapper.eq(ObjectUtils.isNotEmpty(collection.getGoodsId()), Collection::getGoodsId, collection.getGoodsId());
        //根据商家id查看商家商品的收藏数量
        queryWrapper.eq(ObjectUtils.isNotEmpty(collection.getAdminId()), Collection::getAdminId, collection.getAdminId());

        int count = collectionService.count(queryWrapper);
        return R.success(Integer.toString(count));
    }
}
